//callback 为0时，unity 传递的函数为null.
mergeInto(LibraryManager.library, {
	initJsStringToBuffer_v2: function() {
		window.StringToBuffer = function(returnStr) {          
                var bufferSize = lengthBytesUTF8(returnStr) + 1;
                var buffer = _malloc(bufferSize);
                stringToUTF8(returnStr, buffer, bufferSize);                
               // console.log(returnStr,bufferSize,buffer,[buffer]); 
                return buffer;
            };
	},
	// 向数据库中添加数据  
	dbAddData_v2: function(pointer, key, data, callback) {
		window.db_addData(UTF8ToString(key), UTF8ToString(data), function(result) {
			if (callback != 0) {
				Module.dynCall_vii(callback, pointer, window.StringToBuffer(result));
			}
		});
	},

	dbAddDataFloat_v2: function(pointer, key, data, callback) {
		window.db_addData(UTF8ToString(key), data, function(result) {
			if (callback != 0) {
				Module.dynCall_vii(callback, pointer, window.StringToBuffer(result));
			}
		});
	},

	dbAddDataInt_v2: function(pointer, key, data, callback) {
		window.db_addData(UTF8ToString(key), data, function(result) {
			if (callback != 0) {
				Module.dynCall_vii(callback, pointer, window.StringToBuffer(result));
			}
		});
	},
	
    dbAddDataFloatArray_v2: function(pointer, key, data, length,callback) {
        var floatArray = new Float32Array(length);
       for(var i = 0; i < length; i++)
       {
            const _a = HEAPF32[(data >> 2) + i]
           // console.log(_a);
            floatArray[i]=_a;
       }
     //  var float32Array2 = new Float32Array(HeapF32.subarray(data, data + 4));
        window.db_addData(UTF8ToString(key), floatArray, function(result) {
            if (callback != 0) {
                Module.dynCall_vii(callback, pointer, window.StringToBuffer(result));
            }
        });
    },
	
	//获取数据
	dbGetData_v2: function(pointer, key, callback) {
		//window.db_getData(UTF8ToString(key),callback);
		window.db_getData(UTF8ToString(key), function(result) {
			var newResult = String(result)
			//console.log("result 0 type:", typeof result, result, "tostring", typeof newResult, newResult)
			Module.dynCall_vii(callback, pointer, window.StringToBuffer(newResult));
		});
	},

	dbGetDataInt_v2: function(pointer, key, callback) {
		window.db_getData(UTF8ToString(key), function(result) {
			//console.log("result 1 type:", typeof result,result)
			if (typeof result === "number") {
				Module.dynCall_vii(callback, pointer, result);
			} else {
				Module.dynCall_vii(callback, pointer, 0);
			}
		});
	},

	dbGetDataFloat_v2: function(pointer, key, callback) {
		window.db_getData(UTF8ToString(key), function(result) {
			//console.log("result 2 type:", typeof result,result)
			if (typeof result === "number") {
				Module.dynCall_vif(callback, pointer, result);
			} else {
				Module.dynCall_vif(callback, pointer, 0);
			}
		});
	},
		dbGetDataFloatArray_v2_old: function(pointer, key, callback) {
    		window.db_getData(UTF8ToString(key), function(result) {
    		
    		    new Promise((resolve, reject) => {
    		            var length = result.length * 4;
                        var arrayBuffer = new ArrayBuffer(length);
                        var dataView = new DataView(arrayBuffer);
                        var uint8Array = new Uint8Array(arrayBuffer);
                        var i = 0;
    		    
    		            function processData() {
                                     for (let j = 0; j < 20000 && i < result.length; j++) {
                                          dataView.setFloat32(i * 4, result[i], true);
                                          i++;
                                      }
                                      if (i < result.length) {
                                          setTimeout(processData, 0);
                                      } else {
                                          var uint8ArrayPointer = Module._malloc(length * Uint8Array.BYTES_PER_ELEMENT);
                                          var unityHeap = new Uint8Array(Module.HEAPU8.buffer, uint8ArrayPointer, length);
                                          unityHeap.set(uint8Array);
                                          resolve({
                                              pointer: uint8ArrayPointer,
                                              length: length
                                          });
                                      }
                        };
    		            processData();
    		    })    
    		    .then((data) => {
                      Module.dynCall_viii(callback, pointer, data.pointer, data.length);
                  })
                  .catch((error) => {
                      Module.dynCall_viii(callback, pointer, 0, 0);
                  });
    		});
    	},
    	
    	dbGetDataFloatArray_v2: function(pointer, key, callback) {
          window.db_getData(UTF8ToString(key), function(result) {
            		
                var floatArray = result;
                var length = floatArray.length;
                var ptr = Module._malloc(length * 4); // Allocate memory and store the pointer
                Module.HEAPF32.set(floatArray, ptr >> 2); 	  
                Module.dynCall_viii(callback, pointer, ptr, length);
              });  
          },

	dbDelData_v2: function(key) {
		window.db_delData(UTF8ToString(key));
	},
	
	
	TestFloatArray: function() {
            var floatArray = new Float32Array([0.1, 0.2, 0.3, 0.4]);
            var length = floatArray.length;
            var ptr = Module._malloc(length * 4); // Allocate memory and store the pointer
            Module.HEAPF32.set(floatArray, ptr >> 2); 
            return ptr;
        },
         freeFloatArrayMemory: function(ptr) {
                Module._free(ptr);
                ptr = 0; // Reset the pointer after freeing the memory
            }

});